package com.zjc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjc.common.R;
import com.zjc.pojo.StuRoom;
import com.zjc.pojo.Student;
import com.zjc.pojo.VisitTable;
import com.zjc.service.VisitTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 周佳成
 * @since 2023-09-17
 */
@RestController
@RequestMapping("/api/visit")
public class VisitTableController {
	@Autowired
	VisitTableService visitTableService;
	@GetMapping("/find")
	public R<Page> GetSturoomPage(int pageNum, int pageSize, String search){
		QueryWrapper<VisitTable> wrapper = new QueryWrapper<>();
		if(search!=null){
			wrapper.eq("stu_name",search);
		}
		Page<VisitTable> page1 = new Page<>(pageNum,pageSize);
		visitTableService.page(page1,wrapper);
		return R.success(page1);
	}

	@PutMapping("/update")
	public R<String> changeStu(@RequestBody VisitTable vis){
		Integer sgid = vis.getSgId();
		Integer stuid = vis.getStuId();
		String stuName = vis.getStuName();
		String sgName = vis.getSgName();
		if(!visitTableService.isMatch4Sgid2name(sgid,sgName)){
			return R.error("宿管编号与姓名不匹配");
		}
		if(!visitTableService.isMatch4Stuid2name(stuid,stuName)){
			return R.error("学生编号与姓名不匹配");
		}
		boolean rows=visitTableService.updateById(vis);
		System.out.println(rows);
		return R.success("修改成功");
	}
	@PostMapping("/add")
	public R<String> addRoom(@RequestBody VisitTable vis){
		Integer sgid = vis.getSgId();
		Integer stuid = vis.getStuId();
		String stuName = vis.getStuName();
		String sgName = vis.getSgName();
		if(!visitTableService.isMatch4Sgid2name(sgid,sgName)){
			return R.error("宿管编号与姓名不匹配");
		}
		if(!visitTableService.isMatch4Stuid2name(stuid,stuName)){
			return R.error("学生编号与姓名不匹配");
		}
		visitTableService.save(vis);
		return R.success("添加成功");
	}
	@DeleteMapping("/delete/{index}")
	public R<String> deleteRoom(@PathVariable Integer index){
		System.out.println(index);
		int rows=visitTableService.deleteVis(index);
		if(rows!=0){
			return R.success("删除成功");
		}
		else{
			return R.error("删除失败");
		}
	}
}

